<?php

class CSellerGiveVirtual{

	private $virtual;
	
	private $integration;
	
	private $uid;
	
	function __construct(){
		
		$this->virtual=new CVirtual();
		
		$this->integration=new CIntegration();
		
	}
	
	public function setUid($uid){
		
		$this->uid=$uid;
		
	}

	
	public function registerGive(){
		
		$registerVirtual=$this->virtual->getDetail('register');
		
		$this->virtual->addVirtual($registerVirtual,"WHERE uid='$this->uid'");
		
		$registerIntegration=$this->integration->getDetail('register');
		
		$this->integration->addIntegration($registerIntegration,"WHERE uid='$this->uid'");
		
		Zend_Session::start();
			
		$advertiseSession=new Zend_Session_Namespace('advertise');
		
		$sid=$advertiseSession->sid;
		
		if($sid!=''){
			
			$member=new CMember();
			
			$ip=CString::getIp();
			
			$memberArray=$member->getUserDetails(array('reg_ip'=>$ip));
			
			if(count($memberArray)==0){
		
				$memberArray=$member->getUserDetails(array("md5( concat( reg_time, '*', uid ) )"=>$sid));
				
				if(count($memberArray)>0){
					
					$intro_register=$this->virtual->getDetail('intro_register');
			
					$introed_register=$this->virtual->getDetail('introed_register');
					
					$this->virtual->addVirtual($intro_register,"WHERE uid='".$memberArray[0]['uid']."'");
					
					$this->virtual->addVirtual($introed_register,"WHERE uid='".$this->uid."'");
					
					
					$intro_register=$this->integration->getDetail('intro_register');
					
					$introed_register=$this->integration->getDetail('introed_register');
		
					$this->integration->addIntegration($intro_register,"WHERE uid='".$memberArray[0]['uid']."'");
					
					$this->integration->addIntegration($intro_register,"WHERE uid='$this->uid'");
					
				}
			
			}
			
		}
		
	}
	
	public function addProductSub(){
		
		$member=new CMember();
		
		$userArray=$member->getUserDetails(array('uid'=>$this->uid));
		
		$productVirtual=$this->virtual->getDetail('post_product');
		
		$productIntegration=$this->integration->getDetail('post_product');
		
		if($userArray[0]['virtual']<$productVirtual){
			
			return 0;
			
		}
		
		if($userArray[0]['integration']<$productIntegration){
			
			return 1;
			
		}
		
		$this->virtual->subVirtual($productVirtual,"WHERE uid='".$this->uid."'");	
		
		$this->integration->subIntegration($productIntegration,"WHERE uid='$this->uid'");
		
		return 2;
		
	}
	
	public function addSellerSuccess($total){
		
		$sellerSuccessVirtual=$this->virtual->getDetail('seller_success');
		
		$this->virtual->subVirtual($sellerSuccessVirtual*$total,"WHERE uid='".$this->uid."'");
		
		
		$sellerSuccessIntegration=$this->integration->getDetail('seller_success');
		
		$this->integration->addIntegration($sellerSuccessIntegration*$total,"WHERE uid='$this->uid'");
		
	}

	
}